﻿#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //新建文件预览标签页
    m_pTabPreview = new TabPreview();
    //添加给标签页控件
    ui->tabWidget->addTab( m_pTabPreview, tr("文件预览") );

    //关联信号和槽函数，更新文件名
    connect( this, &MainWindow::fileNameChanged,
            m_pTabPreview, &TabPreview::onFileNameChanged);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButtonOpenFile_clicked()
{
    QString strName = QFileDialog::getOpenFileName(this, tr("选择文件"),
                                                   tr(""), tr("All files(*)") );
    strName = strName.trimmed();  //剔除空格
    if( strName.isEmpty() )
    {
        return; //没有文件名
    }
    //获取了文件名
    m_strFileName = strName;
    m_fileInfo.setFile( m_strFileName );
    //文件大小
    qint64 nFileSize = m_fileInfo.size();

    //设置“文件名称”的标签页控件
    ui->lineEditFullName->setText( m_strFileName );
    ui->lineEditShortName->setText( m_fileInfo.fileName() );
    ui->lineEditSize->setText( tr("%1 字节").arg( nFileSize ) );

    //三个时间
    QString strTimeCreated = m_fileInfo.birthTime().toString("yyyy-MM-dd  HH:mm:ss");
    QString strTimeRead = m_fileInfo.lastRead().toString("yyyy-MM-dd  HH:mm:ss");
    QString strTimeModified = m_fileInfo.lastModified().toString("yyyy-MM-dd  HH:mm:ss");

    //设置“文件时间”的标签页控件显示
    ui->lineEditTimeCreated->setText( strTimeCreated );
    ui->lineEditTimeRead->setText( strTimeRead );
    ui->lineEditTimeModify->setText( strTimeModified );

    //触发文件名称修改的信号
    emit fileNameChanged(m_strFileName);
}

